home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmigaPlus / Tools / Development / AmigaTalk / general / LongInteger.st < prev    next >
Encoding:
Text File  |  2004-01-31  |  4.0 KB  |  144 lines

  1. " ------------------------------------------------------------------------ "
  2. " LongInteger Class is for 64-Bit integer representation.  Since there are "
  3. " four functions in utility.library that produce 64-bit quantities, I felt "
  4. " that a separate Class should make use of them.                           "
  5. ""
  6. " signed32BitDivide     is really the SDivMod32() function.                "
  7. " unsigned32BitDivide   is really the UDivMod32() function.                "
  8. " signed64BitMultiply   is really the SMult64()   function.                "
  9. " unsigned64BitMultiply is really the UMult64()   function.                "
  10. ""
  11. " NOTE: "
  12. " Primitives for addition & subtraction will be added later.               "
  13. " ------------------------------------------------------------------------ "
  14.  
  15. Class LongInteger :Number ! upper32Bits lower32Bits !
  16. [
  17.    = aNumber ! chk !
  18.  
  19.       (aNumber isMemberOf: LongInteger)
  20.          ifFalse: [ ^ false ].  "Only valid response for unequal Object Classes"
  21.  
  22.       chk <- <primitive 16 (self getLower32Bits) (aNumber getLower32Bits)>.
  23.  
  24.       (chk ~= true)
  25.          ifTrue: [ ^ false ].
  26.                        
  27.       ^ <primitive 16 (self getUpper32Bits) (aNumber getUpper32Bits)>
  28. |
  29.    > aNumber ! chk !
  30.  
  31.       (aNumber isMemberOf: LongInteger)
  32.          ifFalse: [ ^ false ].   " I might allow comparing to Float later. "
  33.          
  34.       chk <- <primitive 13 (self getLower32Bits) (aNumber getLower32Bits)>.
  35.  
  36.       (chk ~= true)
  37.          ifTrue:  [ ^ false ]
  38.          ifFalse: [ ^ <primitive 13 (self getUpper32Bits) (aNumber getUpper32Bits)> ]
  39. |
  40.    < aNumber ! chk !
  41.  
  42.       (aNumber isMemberOf: LongInteger)
  43.          ifFalse: [ ^ false ].   " I might allow comparing to Float later. "
  44.          
  45.       chk <- <primitive 12 (self getLower32Bits) (aNumber getLower32Bits)>.
  46.  
  47.       (chk ~= true)
  48.          ifTrue:  [ ^ false ]
  49.          ifFalse: [ ^ <primitive 12 (self getUpper32Bits) (aNumber getUpper32Bits)> ]
  50. |
  51.    asString ! highBits !
  52.  
  53.       highBits <- <primitive 37 (self getUpper32Bits)>.
  54.  
  55.       " Concatenate the lower 32-Bits onto the upper 32-Bits: "
  56.  
  57.       ^ (highBits, <primitive 37 (self getLower32Bits)>)
  58. |
  59.    asFloat ! fl1 fl2 !
  60.  
  61.       fl1 <- <primitive 39 (self getUpper32Bits)>.
  62.       fl2 <- <primitive 39 (self getLower32Bits)>.
  63.       
  64.       ^ (fl1 + fl2)
  65. |
  66.    even ! chk !
  67.  
  68.       chk <- self signed32BitDivide: self by: 2. " Or should this be unsigned? "
  69.       
  70.       ^ chk = 0
  71. |
  72.    odd ! chk !
  73.  
  74.       chk <- self signed32BitDivide: self by: 2. " Or should this be unsigned? "
  75.       
  76.       ^ chk ~= 0
  77. |
  78.    getLower32Bits
  79.  
  80.       ^ lower32Bits
  81. |
  82.    getUpper32Bits
  83.  
  84.       ^ upper32Bits         
  85. |
  86.    signed32BitDivide: dividend by: divisor ! result !
  87.  
  88.       " dividend & divisor are 32-Bit Integers: "
  89.       result      <- <primitive 209 3 21 dividend divisor>.
  90.  
  91.       " upper32Bits is really the Quotient: "
  92.       upper32Bits <- <primitive 209 3 39 result>.
  93.  
  94.       " lower32Bits is really the Remainder: "
  95.       lower32Bits <- <primitive 209 3 38 result>.
  96.       
  97.       ^ result
  98. |
  99.    unsigned32BitDivide: dividend by: divisor ! result !
  100.  
  101.       " dividend & divisor are 32-Bit Integers: "
  102.       result      <- <primitive 209 3 22 dividend divisor>.
  103.  
  104.       " upper32Bits is really the Quotient: "
  105.       upper32Bits <- <primitive 209 3 39 result>.
  106.  
  107.       " lower32Bits is really the Remainder: "
  108.       lower32Bits <- <primitive 209 3 38 result>.
  109.       
  110.       ^ result
  111. |
  112.    signed64BitMultiply: arg1 times: arg2 ! result !
  113.  
  114.       " arg1 & arg2 are NOT necessarily 64-bit Integers: "
  115.  
  116.       result      <- <primitive 209 3 23 arg1 arg2>.
  117.  
  118.       upper32Bits <- <primitive 209 3 39 result>.
  119.  
  120.       lower32Bits <- <primitive 209 3 38 result>.
  121.       
  122.       ^ result
  123. |
  124.    unsigned64BitMultiply: arg1 times: arg2 ! result !
  125.  
  126.       " arg1 & arg2 are NOT necessarily 64-bit Integers: "
  127.  
  128.       result      <- <primitive 209 3 24 arg1 arg2>.
  129.  
  130.       upper32Bits <- <primitive 209 3 39 result>.
  131.  
  132.       lower32Bits <- <primitive 209 3 38 result>.
  133.       
  134.       ^ result
  135. |
  136.    quotientIs
  137.  
  138.       ^ upper32Bits
  139. |
  140.    remainderIs
  141.  
  142.       ^ lower32Bits
  143. ]
  144.